package com.ita.osc.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ita.osc.bean.Dish;
import com.ita.osc.dao.DishDao;
import com.ita.osc.db.DbConnection;
import com.ita.osc.util.UUIDUtil;

public class DishDaoImpl implements DishDao {
	
//	private static List<Dish> dishList = new ArrayList<>();
	
//	static{
//		for(int i=0; i<20; i++){
//			dishList.add(new Dish(UUIDUtil.randomUUID(), "Dish" + i, i * 20.2, "00" + ((int)(Math.random()*5) + 1) + ".jpg"));
//		}
//	}
	
//	public void addDish(){
//	for(int i=0; i<20; i++){
//	int m = 0;
////	dishList.add(new Dish(UUIDUtil.randomUUID(), "Dish" + i, i * 20.2, "00" + ((int)(Math.random()*5) + 1) + ".jpg"));
//	String sql = "INSERT INTO dish (did, dname, price, img) VALUES (?, ?, ?, ?)";
//	Connection con = null;
//	PreparedStatement pst = null;
//	ResultSet rs = null;
//	try {
//		con = DbConnection.getConnection();
//	} catch (SQLException e1) {
//		// TODO Auto-generated catch block
//		e1.printStackTrace();
//	}
//	try {
//		pst = con.prepareStatement(sql);
//		pst.setString(1, UUIDUtil.randomUUID());
//		pst.setString(2, "Dish" + i);
//		pst.setDouble(3, i * 20.2);
//		pst.setString(4, "00" + ((int)(Math.random()*5) + 1) + ".jpg");
//		m = pst.executeUpdate();
//	} catch (SQLException e) {
//		e.printStackTrace();
//	} finally {
//		try {
//			con.close();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
//}
//	}

	@Override
	public List<Dish> listAllDish() {
		// TODO Auto-generated method stub
		List<Dish> list = new ArrayList<Dish>();
		String sql = "SELECT * FROM dish";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			Dish dish=null;
			while (rs.next()) {
				String did = rs.getString("did");
				String dname = rs.getString("dname");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				dish = new Dish(did, dname, price, img);
				list.add(dish);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public Dish findDishById(String did) {
		// TODO Auto-generated method stub
		Dish dish=null;
		String sql = "SELECT * FROM dish where did = ?";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = DbConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, did);
			rs = pst.executeQuery();
			while (rs.next()) {
				String dname = rs.getString("dname");
				double price = rs.getDouble("price");
				String img = rs.getString("img");
				dish = new Dish(did, dname, price, img);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return dish;
	}

	@Override
	public Dish findDishByName(String dname) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateDish(Dish dish) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int removeDish(String did) {
		// TODO Auto-generated method stub
		return 0;
	}

}
